<?php
require_once "includes/config.php";
require_once "includes/functions.php";
require_once('AmazonAPI/.config.inc.php');
require_once('AmazonAPI/CBUI/CBUIMultiUsePipeline.php');
session_start();
$sessid=session_id();
$amount=0;
//arrays
$recipient_token_arr = array();
$recipient_amount_arr = array();
$tempID_arr = array();
$item_arr = array();
$itemname_arr = array();
$qty_arr = array();
$temp_tax_arr = array();
$temp_item_code==array();
$extra=0;

$user_id = $_SESSION['user_id_sess'];

$trans  = $_REQUEST['trans'];
$sql= "select order_id,order_amount,item_id,sales_tax,ordered_by,item_code,order_comm,order_comm_to_user_id,order_comm_by_user_id from tbl_orders where transaction_ID='$trans'";

$qry = mysql_query($sql) or die(mysql_error());
$cnt = mysql_num_rows($qry);

if($cnt > 0)
{
	$order = mysql_fetch_array($qry);
	
	
	
	
	$sales_tax 		= $order['sales_tax'];
	$ordered_by		= $order['ordered_by'];
	$item_id 		= $order['item_id'];
	$item_code 		= $order['item_code'];
	$order_comm		= $order['order_comm'];
	$order_comm_by_user_id	= $order['order_comm_by_user_id'];
	
	
	$i_qry = mysql_query("select owner_id,seller_id from tbl_items where item_id=$item_id") or die(mysql_error());
	$c_qry = mysql_num_rows($i_qry);
	if($c_qry > 0)
	{
		$selitem = mysql_fetch_array($i_qry);
		if($selitem['owner_id']==$user_id) 
		{
			//CODE TO REFUND BUYER HERE
			$sql = mysql_query("select tokenID from tbl_users where user_id=$ordered_by") or die(mysql_error());
			$user_row = mysql_fetch_array($sql);
			$amount = $order['order_amount'];
			$extra=number_format((int)$order['order_amount']+(int)$order['sales_tax'],2);
		}
		else
		{
			//CODE TO REFUND COMMISSION TO OWNER HERE
			$sql = mysql_query("select tokenID from tbl_users where user_id=$order_comm_by_user_id") or die(mysql_error());
			$user_row = mysql_fetch_array($sql);
			$amount = $order_comm;
			$extra=number_format((int)$order_comm,2);
		}
	}
	
	
	
	$recipient_token_arr[] = $user_row['tokenID'];
	
	$recipient_amount_arr[] = $extra;
	$tempID_arr[] = $trans;
	$item_arr[] = $item_id;
	$itemname_arr[] = 'Refund';
	$qty_arr[] = 0;
	$temp_tax_arr[] = 0;
	$temp_item_code[]=$item_code;
	
	$_SESSION['recipient_tokens'] = $recipient_token_arr;
	//print_r($recipient_token_arr);
	$_SESSION['recipient_amounts'] = $recipient_amount_arr;
	$_SESSION['tempID_arr'] = $tempID_arr;
	$_SESSION['item_arr'] = $item_arr;
	$_SESSION['itemname_arr'] = $itemname_arr;
	$_SESSION['qty_arr'] = $qty_arr;
	$_SESSION['sales_tax_arr'] =$temp_tax_arr;
	$_SESSION['item_code'] =$temp_item_code;
	
	$amount=$extra;
	
	
	
	$pipeline = new Amazon_FPS_CBUIMultiUsePipeline(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
	
	
	$pipeline->setMandatoryParameters("swapen".rand(),  
		H_ROOT."process_amazon2.php", $amount);
			
	$pipeline->setRecipientTokenList(false, $recipient_token_arr);
	//optional parameters
	$pipeline->addParameter("paymentMethod", "CC");
	$re_URL = $pipeline->getUrl();
	header("location:".$re_URL);
	?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Swapen.com - Buy and Sell</title>
	<style type="text/css">
	<!--
	.style1 {font-weight: bold}
	.style2 {color:#fefefe; font-size:12px; font-family: "Trebuchet MS"; background-color:#000000;}
	.style3 {font-size:14px; color: #4a4a4a; font-family: "Trebuchet MS";}
	-->
	</style>
	
	</head>
	
	<body>
	<div align="center" class="style1"><span class="style2">Please wait, you are being redirected to selected payment gateway</span><span class="style3">.</span></div>
	
	</body>
	</html>
	<?php
	
}
else {
	echo "<script type='text/javascript'> alert('Sorry! This transaction cannot be refunded'); window.location='index.php?mySwapen'; </script>";
}

 



